#include "../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>

pprmq::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        emp = std::make_shared<pprmq::ExchangeManager>("./data/test.db");
        // std::cout << "单元测试执行前的环境初始化" << std::endl;
    }
    virtual void TearDown() override
    {
        // emp->clear();
        std::cout << "单元测试执行完毕后的环境清理" << std::endl;
    }
};

TEST(exchange_test,insert_test)
{
    google::protobuf::Map<std::string, std::string> map;
    map.insert({"k1", "v1"});
    map.insert({"k2", "v2"});
    emp->declareExchange("exchange1",pprmq::ExchangeType::DIRECT,true,false,map);
    emp->declareExchange("exchange2",pprmq::ExchangeType::DIRECT,true,false,map);
    emp->declareExchange("exchange3",pprmq::ExchangeType::DIRECT,true,false,map);
    emp->declareExchange("exchange4",pprmq::ExchangeType::DIRECT,true,false,map);
    ASSERT_EQ(emp->size(),4);
}

TEST(exchange_test,select_test)
{
    ASSERT_EQ(emp->exists("exchange1"),true);
    ASSERT_EQ(emp->exists("exchange2"),true);
    ASSERT_EQ(emp->exists("exchange3"),true);
    ASSERT_EQ(emp->exists("exchange4"),true);

    pprmq::Exchange::ptr exp = emp->selectExchange("exchange1");
    ASSERT_NE(emp.get(),nullptr); // 这个智能指针已经指向了一个有效对象，不是空指针
    ASSERT_EQ(exp->_name,"exchange1");
    ASSERT_EQ(exp->_type,pprmq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->_durable,true);
    ASSERT_EQ(exp->_auto_delete,false);
    ASSERT_EQ(exp->getArgs(),std::string("k1=v1&k2=v2&"));

}

TEST(exchange_test,remove_test)
{
    emp->deleteExchange("exchange2");
    pprmq::Exchange::ptr exp = emp->selectExchange("exchange2");
    ASSERT_EQ(exp.get(),nullptr); // 删除了exchange2，此时就拿不到，应该是空
    ASSERT_EQ(emp->exists("exchange2"),false);
}

int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    return RUN_ALL_TESTS();
}